﻿using CheckIn.ToolsHost.Dtos;
using CheckIn.ToolsHost.Infrastructures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace CheckIn.ToolsHost.Applications
{
    public class FortuneTellingService
    {
        private readonly StrokeCountService _strokeCountService;
        private readonly NumberResultService _numberResultService;
        private readonly FiveElementService _fiveElementService;

        public FortuneTellingService(StrokeCountService strokeCountService,NumberResultService numberResultService, FiveElementService fiveElementService)
        {
            _strokeCountService = strokeCountService;
            _numberResultService = numberResultService;
            _fiveElementService = fiveElementService;
        }

        /// <summary>
        /// 算命
        /// </summary>
        /// <param name="xing">姓</param>
        /// <param name="name">名</param>
        /// <returns></returns>
        public FortuneResult FortuneTelling(string xing,string ming)
        {
            if (string.IsNullOrEmpty(xing) || string.IsNullOrEmpty(ming))
                throw new Exception("姓氏和名字都不能为空");
            var result = new FortuneResult();
            result.Words = new string[4];
            int[] data = new int[4];
            int minus = 0;//总格需要减去多少笔画

            if(xing.Length == 1 && ming.Length >= 3)
            {
                data[0] = _strokeCountService.StrokeCount(xing);
                result.Words[0] = xing;
                for (int i = 0; i < 3; i ++)
                {
                    result.Words[i + 1] = ming.Substring(i, 1);
                    data[i+1] = _strokeCountService.StrokeCount(ming.Substring(i, 1));
                }
            }
            else
            {
               

                if(xing.Length == 1)
                {
                    data[0] = 1;
                    data[1] = _strokeCountService.StrokeCount(xing);
                    result.Words[1] = xing;
                    minus++;
                }
                else
                {
                    result.Words[0] = xing.Substring(0, 1);
                    result.Words[1] = xing.Substring(1, 1);
                    data[0] = _strokeCountService.StrokeCount(xing.Substring(0, 1));
                    data[1] = _strokeCountService.StrokeCount(xing.Substring(1, 1));
                }

                if (ming.Length == 1)
                {
                    result.Words[2] = ming;
                    data[2] = _strokeCountService.StrokeCount(ming);
                    data[3] = 1;
                }
                else
                {
                    result.Words[2] = ming.Substring(0, 1);
                    result.Words[3] = ming.Substring(1, 1);
                    data[2] = _strokeCountService.StrokeCount(ming.Substring(0, 1));
                    data[3] = _strokeCountService.StrokeCount(ming.Substring(1, 1));
                }
            }
           

          
            //天格
            result.Tian = new FortuneItems() { 
                StrokeCount = data[0] + data[1],
            };
            result.Tian.Comments = _numberResultService.GetResult(result.Tian.StrokeCount);
            result.Tian.FiveElement = _fiveElementService.GetFiveElement(result.Tian.StrokeCount);

            //人格
            result.Ren = new FortuneItems()
            {
                StrokeCount = data[1] + data[2]
            };
            result.Ren.Comments = _numberResultService.GetResult(result.Ren.StrokeCount);
            result.Ren.FiveElement = _fiveElementService.GetFiveElement(result.Ren.StrokeCount);

            //地格
            result.Di = new FortuneItems()
            {
                StrokeCount = data[2] + data[3]
            };
            result.Di.Comments = _numberResultService.GetResult(result.Di.StrokeCount);
            result.Di.FiveElement = _fiveElementService.GetFiveElement(result.Di.StrokeCount);

            //外格
            result.Wai = new FortuneItems()
            {
                StrokeCount = data[0] + data[3]
            };
            result.Wai.Comments = _numberResultService.GetResult(result.Wai.StrokeCount);
            result.Wai.FiveElement = _fiveElementService.GetFiveElement(result.Wai.StrokeCount);

            //总格
            result.Zong = new FortuneItems()
            {
                StrokeCount = data.Sum() - minus
            };
            result.Zong.Comments = _numberResultService.GetResult(result.Zong.StrokeCount);
            result.Zong.FiveElement = _fiveElementService.GetFiveElement(result.Zong.StrokeCount);
            result.WordStrokeCounts = data;
            return result;
        }
    }
}
